﻿using System;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using ZXing;
using ThoughtWorks.QRCode.Codec;
using System.Collections.Generic;
using System.Data;
using YTBZ.PUB;

namespace YTBZ
{
    public class BarCode
    {
        //public enum TYPE : int { UNSPECIFIED, UPCA, UPCE, UPC_SUPPLEMENTAL_2DIGIT, UPC_SUPPLEMENTAL_5DIGIT, EAN13, EAN8, Interleaved2of5, Standard2of5, Industrial2of5, CODE39, CODE39Extended, Codabar, PostNet, BOOKLAND, ISBN, JAN13, MSI_Mod10, MSI_2Mod10, MSI_Mod11, MSI_Mod11_Mod10, Modified_Plessey, CODE11, USD8, UCC12, UCC13, LOGMARS, CODE128, CODE128A, CODE128B, CODE128C, ITF14, CODE93, TELEPEN, FIM, PHARMACODE };

        /// <summary>
        /// 生成条形码图片
        /// </summary>
        /// <param name="strBarcode">条形码编号</param>
        /// <param name="strFilePathBarcode">条形码图片文件名</param>
        /// <param name="iPrintType">1：一维码 2：二维码</param>
        /// <param name="isShowNum">一维码显示方式true横false竖,是否显示数据文本</param>
        /// <param name="iHeight">图片高</param>
        /// <param name="iWidth">图片长</param>
        /// <param name="FilePath">图片保存路径</param>
        /// <returns></returns>
        public static string proGetBarCode(string strBarcode, string strFilePathBarcode, int iPrintType, bool isShowNum, int iHeight, int iWidth, string FilePath)
        {
            string strFilePath = "";
            if (string.IsNullOrEmpty(strBarcode) == true)
            {
                return null;
            }
            Barcode b = new Barcode();
            if (iPrintType == 1)
            {
                #region 设置一维条形码图片
                strFilePath = FilePath + "YIWEIMA" + "\\" + DateTime.Now.Year+ "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" + DateTime.Now.Hour;
                if (!Directory.Exists(strFilePath))
                {
                    Directory.CreateDirectory(strFilePath);
                }
                strFilePath = strFilePath + "\\" + strFilePathBarcode + ".bmp";
                int iHight = iHeight;  //畫布高  
                int iy = iWidth;     //筆 y  

                //if (!File.Exists(strFilePath.Trim()))
                //{
                b.IncludeLabel = isShowNum;
                Image imagebarcode=b.Encode(PUB.TYPE.CODE128, strBarcode, iWidth, iHeight);
                imagebarcode.Save(strFilePath,ImageFormat.MemoryBmp);
                imagebarcode.Dispose();
                //Bitmap bitmap = null;
                //bitmap = BarCode.GetBarCodeBitmap("code128b", strBarcode, strFilePath, iHight, iy, isShowNum);   //code39码
                //bitmap.Save(strFilePath);
                //bitmap.Dispose();
                //}
                #endregion
            }
            else if (iPrintType == 2)
            {
                #region 设置二维码图片
                //try
                //{

                string strFolder = "";
                Bitmap bitmap = null;
                //if (strBarcode.Length >= 1 && strBarcode[0] >= 'A' && strBarcode[0] <= 'z')
                //    strFolder = strBarcode.Substring(0, 1) + "\\";
                //else if (strBarcode.Length >= 4 && strBarcode[0] >= '0' && strBarcode[0] <= '9')
                //    strFolder = strBarcode.Substring(0, 4) + "\\";
                strFilePath = FilePath + "ERWEIMA\\"  + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" + DateTime.Now.Hour;
                if (!Directory.Exists(strFilePath))
                {
                    Directory.CreateDirectory(strFilePath);
                }
                strFilePath = strFilePath + "\\" + strFilePathBarcode + ".bmp";
                //if (!File.Exists(strFilePath.Trim()))
                //{
                bitmap = GenByZXingNet(strBarcode, iHeight, iWidth);
                bitmap.Save(strFilePath);
                bitmap.Dispose();
                //}

                //}
                //catch
                //{
                //    return "";
                //}
                #endregion
            }

            //FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
            //BinaryReader br = new BinaryReader(fs);
            //int fileLength = (int)fs.Length;
            //Byte[] fileByte = new Byte[fileLength];
            //br.Read(fileByte, 0, fileLength);
            //fs.Close();
            //br.Close();
            return strFilePath;
        }
        #region 生成条形码图片，支持code39,code93,code128三种
        /// <summary>
        /// 生成条形码图片，支持code39,code93,code128三种
        /// </summary>
        /// <param name="CodeMode">类型：code39,code93,code128C </param>
        /// <param name="mycode">条码值</param>
        /// <param name="strFilePath">图片保存路径</param>
        /// <param name="iHight">畫布高</param>
        /// <param name="iy">筆 y</param>
        /// <param name="isShowNum">code93是否显示数字  code39\128显示方式true横false竖</param> 
        public static Bitmap GetBarCodeBitmap(string CodeMode, string mycode, string strFilePath, int iHight, int iy, bool isShowNum)
        {
            Bitmap BMP = null;
            Image img = BMP;
            switch (CodeMode.ToLower())
            {
                //case "code39_1":
                //    GetBarCodeBitmap_Code39_1(mycode, strFilePath, iHight, iy, isShowNum);
                //    break;
                case "code39": //可行
                    if (isShowNum)
                    {
                        BMP = GetBarCodeBitmap_Code39(mycode, strFilePath, iHight, iy, isShowNum);
                    }
                    else BMP = GetBarCodeBitmapZ90_Code39(mycode, strFilePath, iHight, iy, isShowNum);
                    break;
                case "code128n":
                    Barcode b = new Barcode();
                    img = b.Encode(TYPE.CODE128, mycode,  550, iHight);
                    BMP=(Bitmap)img;
                    break;
                case "code128c1":
                    BMP = GetBarCodeBitmap_Code128(mycode, strFilePath, iHight, isShowNum);
                    break;
                case "code128a":  //可行
                    if (!string.IsNullOrEmpty(mycode))
                    {
                        Code128 code128A = new Code128();
                        BMP = code128A.GetCodeImage(mycode, Code128.Encode.Code128A, isShowNum);
                        //string Folder = strFilePath.Remove(strFilePath.LastIndexOf("\\"));
                        //if (!Directory.Exists(Folder))//判断文件夹是否存在
                        //{
                        //    Directory.CreateDirectory(Folder);
                        //}
                        //BMP128A.Save(strFilePath, ImageFormat.Jpeg);
                        //BMP128A.Dispose();
                    }
                    break;
                case "code128b": //可行
                    if (!string.IsNullOrEmpty(mycode))
                    {
                        Code128 code128B = new Code128();
                        BMP = code128B.GetCodeImage(mycode, Code128.Encode.Code128B, isShowNum);
                        //BMP128B.Save(strFilePath, ImageFormat.Jpeg);
                        //BMP128B.Dispose();

                    }
                    break;
                case "ean128":
                    if (!string.IsNullOrEmpty(mycode))
                    {
                        Code128 ean128 = new Code128();
                        BMP = ean128.GetCodeImage(mycode, Code128.Encode.EAN128, isShowNum);
                        //BMPean128.Save(strFilePath, ImageFormat.Jpeg);
                        //BMPean128.Dispose();

                    }
                    break;
                case "code128c":
                    if (!string.IsNullOrEmpty(mycode))
                    {
                        Code128 code128C = new Code128();
                        BMP = code128C.GetCodeImage(mycode, Code128.Encode.Code128C, isShowNum);
                        //BMP128C.Save(strFilePath, ImageFormat.Jpeg);
                        //BMP128C.Dispose();
                    }
                    break;
                default: break;
            }
            return BMP;
        }
        #endregion

        #region GetBarCodeBitmap_Code128  条码 Code 128码

        /// <summary>
        /// 条形码生成函数Code 128码
        /// </summary> 
        /// <param name="mycode">条型码字串</param>
        /// <param name="strFilePath">图片路径</param>
        /// <param name="iWidth">条码宽度</param>
        /// <param name="isShowMode">显示方式true横false竖</param>
        /// <returns></returns>
        public static Bitmap GetBarCodeBitmap_Code128(string mycode, string strFilePath, int iWidth, bool isShowMode)
        {
            Bitmap BMP = null;
            if (!string.IsNullOrEmpty(mycode))
            {
                //string strZC;//字串
                //             //strZC = "*-%$*"
                //strZC = "*" + mycode + "*";  //Code 39 的特性是前、後置碼會標識「星號(*)」，表示開始和結束

                //int iHight = 0;  //畫布高
                //                 //int iWidth = 60;   //畫布寬
                //                 //int ix = 40;      //筆 x
                //                 //int 筆寬 = 0;
                //iHight = strZC.Length * 13;

                BMP = BuildBarCode128(mycode, iWidth, isShowMode);

                //Encoder myEncoder = Encoder.Quality;
                //EncoderParameters myEncoderParameters = new EncoderParameters(1);
                //// 保存位图的JPEG文件的质量水平25。 
                //EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 75L);
                //ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
                //myEncoderParameters.Param[0] = myEncoderParameter;
                //BMP.Save(strFilePath, myImageCodecInfo, myEncoderParameters);

                //BMP.Dispose();
            }

            return BMP;
        }
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        /// <summary>
        /// 条形码生成函数
        /// </summary> 
        /// <param name="mycode">条型码字串</param>
        /// <param name="iHight">高或宽</param>
        /// <param name="isShowMode">显示方式true横false竖</param>
        /// <returns></returns>
        public static Bitmap BuildBarCode128(string mycode, int iHight, bool isShowMode)
        {

            //查检是否合条件TEXT

            //bool ck = CheckErrerCode(text);

            //if (!ck)
            //    throw new Exception("条形码字符不合要求，不能是汉字或全角字符");

            string barstring = BuildBarString(mycode);
            //if (!isShowMode)
            //    return KiCode128CZ90(barstring, iHight);//垂直条码
            //else
            return KiCode128C(barstring, mycode, iHight);//平行条码
        }

        /// <summary>
        /// 建立条码字符串
        /// </summary>
        /// <param name="tex">条码内容</param>
        /// <returns></returns>
        private static string BuildBarString(string tex)
        {

            string barstart = "bbsbssbssss";    //码头

            string barbody = "";                //码身

            string barcheck = "";               //码检

            string barend = "bbsssbbbsbsbb";    //码尾

            int checkNum = 104;

            //循环添加码身,计算码检

            for (int i = 1; i <= tex.Length; i++)
            {

                int index = (int)tex[i - 1] - 32;

                checkNum += (index * i);

                barbody += AddSimpleTag(index);//加入字符值的条码标记

            }

            //码检值计算

            barcheck = AddSimpleTag(int.Parse(Convert.ToDouble(checkNum % 103).ToString("0")));

            string barstring = barstart + barbody + barcheck + barend;

            return barstring;

        }

        //增加一个条码标记
        private static string AddSimpleTag(int CodeIndex)
        {

            string res = "";
            /// <summary>1-4的条的字符标识 </summary>
            string[] TagB = { "", "b", "bb", "bbb", "bbbb" };
            /// <summary>1-4的空的字符标识 </summary>
            string[] TagS = { "", "s", "ss", "sss", "ssss" };

            string[] Code128List = new string[] {

                "212222","222122","222221","121223","121322","131222","122213","122312","132212","221213",

                "221312","231212","112232","122132","122231","113222","123122","123221","223211","221132",

                "221231","213212","223112","312131","311222","321122","321221","312212","322112","322211",

                "212123","212321","232121","111323","131123","131321","112313","132113","132311","211313",

                "231113","231311","112133","112331","132131","113123","113321","133121","313121","211331",

                "231131","213113","213311","213131","311123","311321","331121","312113","312311","332111",

                "314111","221411","431111","111224","111422","121124","121421","141122","141221","112214",

                "112412","122114","122411","142112","142211","241211","221114","413111","241112","134111",

                "111242","121142","121241","114212","124112","124211","411212","421112","421211","212141",

                "214121","412121","111143","111341","131141","114113","114311","411113","411311","113141",

                "114131","311141","411131","211412","211214","211232" };

            string tag = Code128List[CodeIndex];

            for (int i = 0; i < tag.Length; i++)
            {

                string temp = "";

                int num = int.Parse(tag[i].ToString());

                if (i % 2 == 0)
                {

                    temp = TagB[num];
                }

                else
                {
                    temp = TagS[num];
                }
                res += temp;
            }

            return res;

        }

        /// <summary>
        /// 检查条形码文字是否合条件(不能是汉字或全角字符)
        /// </summary>
        /// <param name="cktext"></param>
        /// <returns></returns>
        private static bool CheckErrerCode(string cktext)
        {
            foreach (char c in cktext)
            {
                byte[] tmp = System.Text.UnicodeEncoding.Default.GetBytes(c.ToString());
                if (tmp.Length > 1)
                    return false;
            }
            return true;
        }

        /// <summary>生成条码128码 垂直</summary>
        /// <param name="BarString">条码模式字符串</param> //Format32bppArgb
        /// <param name="Height">生成的条码高度</param>
        /// <returns>条码图形</returns>
        private static Bitmap KiCode128CZ90(string BarString, int iWidth)
        {
            Bitmap b = new Bitmap(iWidth, BarString.Length, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
            using (Graphics grp = Graphics.FromImage(b))
            {
                try
                {
                    char[] cs = BarString.ToCharArray();
                    for (int i = 0; i < cs.Length; i++)
                    {
                        for (int j = 0; j < iWidth; j++)
                        {
                            if (cs[i] == 'b')
                            {
                                b.SetPixel(j, i, Color.Black);
                            }
                            else
                            {
                                b.SetPixel(j, i, Color.White);
                            }

                        }

                    }
                    //grp.DrawString(text, SystemFonts.CaptionFont, Brushes.Black, new PointF(leftEmpty, b.Height - botEmpty));
                    return b;
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>生成条码128码 平行 </summary>
        /// <param name="BarString">条码模式字符串</param> //Format32bppArgb
        /// <param name="Height">生成的条码高度</param>
        /// <param name="text">码</param>
        /// <returns>条码图形</returns>
        private static Bitmap KiCode128C(string BarString, string text , int _Height)
        {
            int leftEmpty = 0;
            int botEmpty = 10;
            Bitmap b = new Bitmap(BarString.Length, _Height, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
            using (Graphics grp = Graphics.FromImage(b))
            {
                try
                {
                    char[] cs = BarString.ToCharArray();
                    for (int i = 0; i < cs.Length; i++)
                    {
                        for (int j = 0; j < _Height; j++)
                        {
                            if (cs[i] == 'b')
                            {
                                b.SetPixel(i, j, Color.Black);
                            }
                            else
                            {
                                b.SetPixel(i, j, Color.White);
                            }

                        }

                    }
                    grp.DrawString(text, SystemFonts.CaptionFont, Brushes.Black, new PointF(leftEmpty, b.Height - botEmpty));
                    return b;
                }
                catch
                {
                    return null;
                }
            }
        }
        #endregion

        #region    GetBarCodeBitmap_Code39 条形码图片 Code39码
        /// <summary>
        /// 生成条形码图片
        /// </summary>
        /// <param name="mycode">条码值</param>
        /// <param name="strFilePath">图片保存路径</param>
        /// <param name="iHight">畫布高</param>
        /// <param name="iy">筆 y</param>
        /// <param name="isShowNum">是否显示数字</param>
        /// <returns></returns>
        private static Bitmap GetBarCodeBitmap_Code39(string mycode, string strFilePath, int iHight, int iy, bool isShowNum)
        {
            //string strFilePath = HttpContext.Current.Application["XDDYFolder"].ToString().Trim('\\') + "\\Code39Img\\" + mycode + ".jpg";

            string strZC;//字串
            string strZY;//字元
            //strZC = "*-%$*"
            strZC = "*" + mycode + "*";  //Code 39 的特性是前、後置碼會標識「星號(*)」，表示開始和結束

            //int iHight = 60;  //畫布高
            int iWidth = 0;   //畫布寬
            int ix = 0;      //筆 x
                             //int iy = 40;     //筆 y
                             //int 筆寬 = 0;
            Bitmap BMP = null;
            if (!string.IsNullOrEmpty(mycode))
            {
                iWidth = strZC.Length * 13;

                BMP = new Bitmap(iWidth, iHight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                Graphics G = Graphics.FromImage(BMP);
                G.TextRenderingHint = TextRenderingHint.AntiAlias;
                G.Clear(Color.White);

                Brush brush1 = new SolidBrush(Color.White);
                G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                G.FillRectangle(brush1, 0, 0, iWidth, iHight);

                for (int i = 0; i < strZC.Length; i++)
                {
                    //取得 Code 39 碼的規則
                    strZY = genBarcode(strZC.Substring(i, 1).ToUpper());

                    for (int j = 0; j < 4; j++)
                    {
                        if (strZY.Substring(j, 1).Equals("0"))
                        {
                            G.DrawLine(Pens.Black, ix, 0, ix, iy);
                        }
                        else
                        {
                            G.DrawLine(Pens.Black, ix, 0, ix, iy);
                            G.DrawLine(Pens.Black, ix + 1, 0, ix + 1, iy);
                            ix += 1;
                        }

                        ix += 1;

                        if (strZY.Substring(j + 5, 1).Equals("0"))
                        {
                            G.DrawLine(Pens.White, ix, 0, ix, iy);
                        }
                        else
                        {
                            G.DrawLine(Pens.White, ix, 0, ix, iy);
                            G.DrawLine(Pens.White, ix + 1, 0, ix + 1, iy);
                            ix += 1;
                        }

                        ix += 1;
                    } //end of loop

                    if (strZY.Substring(4, 1).Equals("0"))
                    {
                        G.DrawLine(Pens.Black, ix, 0, ix, iy);
                    }
                    else
                    {
                        G.DrawLine(Pens.Black, ix, 0, ix, iy);
                        G.DrawLine(Pens.Black, ix + 1, 0, ix + 1, iy);
                        ix += 1;
                    }

                    ix += 2;
                } //end of loop
                if (!isShowNum) //是否显示数字
                {
                    int x = 0;
                    int addx = 13;

                    //G.DrawString("-", new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));
                    x += addx;

                    for (int k = 0; k < mycode.Length; k++)
                    {
                        G.DrawString(mycode.Substring(k, 1), new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, iy));
                        x = x + addx;
                    }
                }
                //G.DrawString("-", new Font("Arial", 10, FontStyle.Italic), SystemBrushes.WindowText, new PointF(x, 20));
                string Folder = strFilePath.Remove(strFilePath.LastIndexOf("\\"));
                if (!Directory.Exists(Folder))//判断文件夹是否存在
                {
                    Directory.CreateDirectory(Folder);
                }
                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);

                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10);
                ps.Param[0] = p;

                //BMP.Save(Response.OutputStream, ImageFormat.Jpeg);
                BMP.Save(strFilePath, ImageFormat.Jpeg);
                //BMP.Save(strFilePath, GetCodecInfo("image/jpeg"), ps);
                G.Dispose();
                BMP.Dispose();
            }
            else
            {
                iWidth = 100;

                BMP = new Bitmap(iWidth, iHight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                Graphics G = Graphics.FromImage(BMP);
                G.TextRenderingHint = TextRenderingHint.AntiAlias;
                G.Clear(Color.White);
                //未給參數時顯示的提示內容
                G.DrawString("无条码产生", new Font("宋体", 12, FontStyle.Regular), SystemBrushes.WindowText, new PointF(0, 20));
                //BMP.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                G.Dispose();

                BMP.Dispose();
            }
            return BMP;
        }

        private static Bitmap GetBarCodeBitmapZ90_Code39(string mycode, string strFilePath, int iWidth, int ix, bool isShowNum)
        {
            //string strFilePath = HttpContext.Current.Application["XDDYFolder"].ToString().Trim('\\') + "\\Code39Img\\" + mycode + ".jpg";

            string strZC;//字串
            string strZY;//字元
            //strZC = "*-%$*"
            strZC = "*" + mycode + "*";  //Code 39 的特性是前、後置碼會標識「星號(*)」，表示開始和結束

            int iHight = 0;  //畫布高
            //int iWidth = 60;   //畫布寬
            //int ix = 40;      //筆 x
            int iy = 0;     //筆 y
                            //int 筆寬 = 0;
            Bitmap BMP = null;
            if (!string.IsNullOrEmpty(mycode))
            {
                iHight = strZC.Length * 13;

                BMP = new Bitmap(iWidth, iHight, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
                Graphics G = Graphics.FromImage(BMP);
                G.TextRenderingHint = TextRenderingHint.AntiAlias;
                G.Clear(Color.White);
                G.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //插值模式
                G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;//文本呈现提示
                Brush brush1 = new SolidBrush(Color.White);
                G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                G.FillRectangle(brush1, 0, 0, iWidth, iHight);

                for (int i = 0; i < strZC.Length; i++)
                {
                    //取得 Code 39 碼的規則
                    strZY = genBarcode(strZC.Substring(i, 1).ToUpper());

                    for (int j = 0; j < 4; j++)
                    {
                        if (strZY.Substring(j, 1).Equals("0"))
                        {
                            G.DrawLine(Pens.Black, 0, iy, ix, iy);
                        }
                        else
                        {
                            G.DrawLine(Pens.Black, 0, iy, ix, iy);
                            G.DrawLine(Pens.Black, 0, iy + 1, ix + 1, iy + 1);
                            iy += 1;
                        }

                        iy += 1;

                        if (strZY.Substring(j + 5, 1).Equals("0"))
                        {
                            G.DrawLine(Pens.White, 0, iy, ix, iy);
                        }
                        else
                        {
                            G.DrawLine(Pens.White, 0, iy, ix, iy);
                            G.DrawLine(Pens.White, 0, iy + 1, ix, iy + 1);
                            iy += 1;
                        }

                        iy += 1;
                    } //end of loop

                    if (strZY.Substring(4, 1).Equals("0"))
                    {
                        G.DrawLine(Pens.Black, 0, iy, ix, iy);
                    }
                    else
                    {
                        G.DrawLine(Pens.Black, 0, iy, ix, iy);
                        G.DrawLine(Pens.Black, 0, iy + 1, ix, iy + 1);
                        iy += 1;
                    }

                    iy += 2;
                } //end of loop

                string Folder = strFilePath.Remove(strFilePath.LastIndexOf("\\"));
                if (!Directory.Exists(Folder))//判断文件夹是否存在
                {
                    Directory.CreateDirectory(Folder);
                }
                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);

                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10);
                ps.Param[0] = p;

                //BMP.Save(Response.OutputStream, ImageFormat.Jpeg);
                //BMP.Save(strFilePath, ImageFormat.Jpeg);
                //BMP.Save(strFilePath, GetCodecInfo("image/jpeg"), ps);
                G.Dispose();
                BMP.Dispose();
            }
            else
            {
                iWidth = 100;

                BMP = new Bitmap(iWidth, iHight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                Graphics G = Graphics.FromImage(BMP);
                G.TextRenderingHint = TextRenderingHint.AntiAlias;
                G.Clear(Color.White);

                //未給參數時顯示的提示內容
                G.DrawString("无条码产生", new Font("宋体", 12, FontStyle.Regular), SystemBrushes.WindowText, new PointF(0, 20));
                //BMP.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                G.Dispose();

                BMP.Dispose();
            }
            return BMP;
        }
        // 規則可參考網址 1：http://blog.csdn.net/xuzhongxuan/archive/2008/05/28/2489358.aspx
        // 規則可參考網址 2：http://blog.163.com/zryou/blog/static/6903184200971704226450/
        /// <summary>
        /// Code 39 碼的規則。
        /// Code 39 碼可使用的strZY如下：0~9、A~Z、+、-、*、/、%、$、. 及空白strZY。    
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static string genBarcode(string code)
        {
            switch (code)
            {
                case "0":
                    code = "001100100";
                    break;
                case "1":
                    code = "100010100";
                    break;
                case "2":
                    code = "010010100";
                    break;
                case "3":
                    code = "110000100";
                    break;
                case "4":
                    code = "001010100";
                    break;
                case "5":
                    code = "101000100";
                    break;
                case "6":
                    code = "011000100";
                    break;
                case "7":
                    code = "000110100";
                    break;
                case "8":
                    code = "100100100";
                    break;
                case "9":
                    code = "010100100";
                    break;
                case "A":
                    code = "100010010";
                    break;
                case "B":
                    code = "010010010";
                    break;
                case "C":
                    code = "110000010";
                    break;
                case "D":
                    code = "001010010";
                    break;
                case "E":
                    code = "101000010";
                    break;
                case "F":
                    code = "011000010";
                    break;
                case "G":
                    code = "000110010";
                    break;
                case "H":
                    code = "100100010";
                    break;
                case "I":
                    code = "010100010";
                    break;
                case "J":
                    code = "001100010";
                    break;
                case "K":
                    code = "100010001";
                    break;
                case "L":
                    code = "010010001";
                    break;
                case "M":
                    code = "110000001";
                    break;
                case "N":
                    code = "001010001";
                    break;
                case "O":
                    code = "101000001";
                    break;
                case "P":
                    code = "011000001";
                    break;
                case "Q":
                    code = "000110001";
                    break;
                case "R":
                    code = "100100001";
                    break;
                case "S":
                    code = "010100001";
                    break;
                case "T":
                    code = "001100001";
                    break;
                case "U":
                    code = "100011000";
                    break;
                case "V":
                    code = "010011000";
                    break;
                case "W":
                    code = "110001000";
                    break;
                case "X":
                    code = "001011000";
                    break;
                case "Y":
                    code = "101001000";
                    break;
                case "Z":
                    code = "011001000";
                    break;
                case "*":
                    code = "001101000";
                    break;
                case "-":
                    code = "000111000"; //好像辨識不出來
                    break;
                case "%":
                    code = "100101000"; //好像辨識不出來
                    break;
                case "$":
                    code = "010101000"; //好像辨識不出來
                    break;
                default:
                    code = "010101000"; //都不是就印 $
                    break;
            }

            return code;
        }

        /// 保存JPG时用
        /// </summary>
        /// <param name="mimeType"></param>
        /// <returns>得到指定mimeType的ImageCodecInfo</returns>
        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if (ici.MimeType == mimeType) return ici;
            }
            return null;
        }

        #endregion

        /// <summary>
        /// 简单二维码
        /// </summary>
        /// <param name="mycode">码值</param>
        /// <returns></returns>
        private static Bitmap GetEWIEMA(string mycode)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            Bitmap bitCode = null;
            #region 设置二维码图片ThoughtWorks.QRCode
            if (!string.IsNullOrEmpty(mycode))
            {

                String encoding = "AlphaNumeric";
                if (encoding == "Byte")
                {
                    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                }
                else if (encoding == "AlphaNumeric")
                {
                    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
                }
                else if (encoding == "Numeric")
                {
                    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
                }
                string errorCorrect = "H";
                if (errorCorrect == "L")
                    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
                else if (errorCorrect == "M")
                    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                else if (errorCorrect == "Q")
                    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
                else if (errorCorrect == "H")
                    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
                try
                {
                    bitCode = qrCodeEncoder.Encode(mycode);

                }
                catch
                {
                    bitCode = null;
                }
            }
            #endregion
            return bitCode;
        }
        #region    生成复杂二维码
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="msg">二维码信息</param>
        /// <returns>图片</returns>
        private static Bitmap GenByZXingNet(string msg, int iHeight, int iWidth)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
            writer.Options.Hints.Add(
                EncodeHintType.ERROR_CORRECTION,
                ZXing.QrCode.Internal.ErrorCorrectionLevel.H

            );
            //const int codeSizeInPixels = 250;   //设置图片长宽
            writer.Options.Height = iHeight;
            writer.Options.Width = iWidth;
            writer.Options.Margin = 0;//设置边框
            ZXing.Common.BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            return img;
        }
        #endregion
    }

    public class Code128
    {
        private DataTable m_Code128 = new DataTable();
        private uint m_Height = 55;
        /// <summary>
        /// 高度
        /// </summary>
        public uint Height { get { return m_Height; } set { m_Height = value; } }
        private Font m_ValueFont = new Font("Arial", 10);
        /// <summary>
        /// 是否显示可见号码  如果为NULL不显示号码
        /// </summary>
        public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
        private byte m_Magnify = 0;
        /// <summary>
        /// 放大倍数
        /// </summary>
        public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
        /// <summary>
        /// 条码类别
        /// </summary>
        public enum Encode
        {
            Code128A,
            Code128B,
            Code128C,
            EAN128
        }
        public Code128()
        {
            m_Code128.Columns.Add("ID");
            m_Code128.Columns.Add("Code128A");
            m_Code128.Columns.Add("Code128B");
            m_Code128.Columns.Add("Code128C");
            m_Code128.Columns.Add("BandCode");
            m_Code128.CaseSensitive = true;
            #region 数据表
            m_Code128.Rows.Add("0", " ", " ", "00", "212222");
            m_Code128.Rows.Add("1", "!", "!", "01", "222122");
            m_Code128.Rows.Add("2", "\"", "\"", "02", "222221");
            m_Code128.Rows.Add("3", "#", "#", "03", "121223");
            m_Code128.Rows.Add("4", "$", "$", "04", "121322");
            m_Code128.Rows.Add("5", "%", "%", "05", "131222");
            m_Code128.Rows.Add("6", "&", "&", "06", "122213");
            m_Code128.Rows.Add("7", "'", "'", "07", "122312");
            m_Code128.Rows.Add("8", "(", "(", "08", "132212");
            m_Code128.Rows.Add("9", ")", ")", "09", "221213");
            m_Code128.Rows.Add("10", "*", "*", "10", "221312");
            m_Code128.Rows.Add("11", "+", "+", "11", "231212");
            m_Code128.Rows.Add("12", ",", ",", "12", "112232");
            m_Code128.Rows.Add("13", "-", "-", "13", "122132");
            m_Code128.Rows.Add("14", ".", ".", "14", "122231");
            m_Code128.Rows.Add("15", "/", "/", "15", "113222");
            m_Code128.Rows.Add("16", "0", "0", "16", "123122");
            m_Code128.Rows.Add("17", "1", "1", "17", "123221");
            m_Code128.Rows.Add("18", "2", "2", "18", "223211");
            m_Code128.Rows.Add("19", "3", "3", "19", "221132");
            m_Code128.Rows.Add("20", "4", "4", "20", "221231");
            m_Code128.Rows.Add("21", "5", "5", "21", "213212");
            m_Code128.Rows.Add("22", "6", "6", "22", "223112");
            m_Code128.Rows.Add("23", "7", "7", "23", "312131");
            m_Code128.Rows.Add("24", "8", "8", "24", "311222");
            m_Code128.Rows.Add("25", "9", "9", "25", "321122");
            m_Code128.Rows.Add("26", ":", ":", "26", "321221");
            m_Code128.Rows.Add("27", ";", ";", "27", "312212");
            m_Code128.Rows.Add("28", "<", "<", "28", "322112");
            m_Code128.Rows.Add("29", "=", "=", "29", "322211");
            m_Code128.Rows.Add("30", ">", ">", "30", "212123");
            m_Code128.Rows.Add("31", "?", "?", "31", "212321");
            m_Code128.Rows.Add("32", "@", "@", "32", "232121");
            m_Code128.Rows.Add("33", "A", "A", "33", "111323");
            m_Code128.Rows.Add("34", "B", "B", "34", "131123");
            m_Code128.Rows.Add("35", "C", "C", "35", "131321");
            m_Code128.Rows.Add("36", "D", "D", "36", "112313");
            m_Code128.Rows.Add("37", "E", "E", "37", "132113");
            m_Code128.Rows.Add("38", "F", "F", "38", "132311");
            m_Code128.Rows.Add("39", "G", "G", "39", "211313");
            m_Code128.Rows.Add("40", "H", "H", "40", "231113");
            m_Code128.Rows.Add("41", "I", "I", "41", "231311");
            m_Code128.Rows.Add("42", "J", "J", "42", "112133");
            m_Code128.Rows.Add("43", "K", "K", "43", "112331");
            m_Code128.Rows.Add("44", "L", "L", "44", "132131");
            m_Code128.Rows.Add("45", "M", "M", "45", "113123");
            m_Code128.Rows.Add("46", "N", "N", "46", "113321");
            m_Code128.Rows.Add("47", "O", "O", "47", "133121");
            m_Code128.Rows.Add("48", "P", "P", "48", "313121");
            m_Code128.Rows.Add("49", "Q", "Q", "49", "211331");
            m_Code128.Rows.Add("50", "R", "R", "50", "231131");
            m_Code128.Rows.Add("51", "S", "S", "51", "213113");
            m_Code128.Rows.Add("52", "T", "T", "52", "213311");
            m_Code128.Rows.Add("53", "U", "U", "53", "213131");
            m_Code128.Rows.Add("54", "V", "V", "54", "311123");
            m_Code128.Rows.Add("55", "W", "W", "55", "311321");
            m_Code128.Rows.Add("56", "X", "X", "56", "331121");
            m_Code128.Rows.Add("57", "Y", "Y", "57", "312113");
            m_Code128.Rows.Add("58", "Z", "Z", "58", "312311");
            m_Code128.Rows.Add("59", "[", "[", "59", "332111");
            m_Code128.Rows.Add("60", "\\", "\\", "60", "314111");
            m_Code128.Rows.Add("61", "]", "]", "61", "221411");
            m_Code128.Rows.Add("62", "^", "^", "62", "431111");
            m_Code128.Rows.Add("63", "_", "_", "63", "111224");
            m_Code128.Rows.Add("64", "NUL", "`", "64", "111422");
            m_Code128.Rows.Add("65", "SOH", "a", "65", "121124");
            m_Code128.Rows.Add("66", "STX", "b", "66", "121421");
            m_Code128.Rows.Add("67", "ETX", "c", "67", "141122");
            m_Code128.Rows.Add("68", "EOT", "d", "68", "141221");
            m_Code128.Rows.Add("69", "ENQ", "e", "69", "112214");
            m_Code128.Rows.Add("70", "ACK", "f", "70", "112412");
            m_Code128.Rows.Add("71", "BEL", "g", "71", "122114");
            m_Code128.Rows.Add("72", "BS", "h", "72", "122411");
            m_Code128.Rows.Add("73", "HT", "i", "73", "142112");
            m_Code128.Rows.Add("74", "LF", "j", "74", "142211");
            m_Code128.Rows.Add("75", "VT", "k", "75", "241211");
            m_Code128.Rows.Add("76", "FF", "I", "76", "221114");
            m_Code128.Rows.Add("77", "CR", "m", "77", "413111");
            m_Code128.Rows.Add("78", "SO", "n", "78", "241112");
            m_Code128.Rows.Add("79", "SI", "o", "79", "134111");
            m_Code128.Rows.Add("80", "DLE", "p", "80", "111242");
            m_Code128.Rows.Add("81", "DC1", "q", "81", "121142");
            m_Code128.Rows.Add("82", "DC2", "r", "82", "121241");
            m_Code128.Rows.Add("83", "DC3", "s", "83", "114212");
            m_Code128.Rows.Add("84", "DC4", "t", "84", "124112");
            m_Code128.Rows.Add("85", "NAK", "u", "85", "124211");
            m_Code128.Rows.Add("86", "SYN", "v", "86", "411212");
            m_Code128.Rows.Add("87", "ETB", "w", "87", "421112");
            m_Code128.Rows.Add("88", "CAN", "x", "88", "421211");
            m_Code128.Rows.Add("89", "EM", "y", "89", "212141");
            m_Code128.Rows.Add("90", "SUB", "z", "90", "214121");
            m_Code128.Rows.Add("91", "ESC", "{", "91", "412121");
            m_Code128.Rows.Add("92", "FS", "|", "92", "111143");
            m_Code128.Rows.Add("93", "GS", "}", "93", "111341");
            m_Code128.Rows.Add("94", "RS", "~", "94", "131141");
            m_Code128.Rows.Add("95", "US", "DEL", "95", "114113");
            m_Code128.Rows.Add("96", "FNC3", "FNC3", "96", "114311");
            m_Code128.Rows.Add("97", "FNC2", "FNC2", "97", "411113");
            m_Code128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311");
            m_Code128.Rows.Add("99", "CODEC", "CODEC", "99", "113141");
            m_Code128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131");
            m_Code128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141");
            m_Code128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131");
            m_Code128.Rows.Add("103", "StartA", "StartA", "StartA", "211412");
            m_Code128.Rows.Add("104", "StartB", "StartB", "StartB", "211214");
            m_Code128.Rows.Add("105", "StartC", "StartC", "StartC", "211232");
            m_Code128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112");
            #endregion
        }
        /// <summary>
        /// 获取128图形
        /// </summary>
        /// <param name="p_Text">文字</param>
        /// <param name="p_Code">编码</param>  
        /// <param name="isShowNum">显示方式true横false竖</param> 
        /// <returns>图形</returns>
        public Bitmap GetCodeImage(string p_Text, Encode p_Code, bool isShowNum)
        {
            string _ViewText = p_Text;
            string _Text = "";
            IList<int> _TextNumb = new List<int>();
            int _Examine = 0;  //首位
            switch (p_Code)
            {
                case Encode.Code128C:
                    _Examine = 105;
                    if (!((p_Text.Length & 1) == 0)) throw new Exception("128C长度必须是偶数");
                    while (p_Text.Length != 0)
                    {
                        int _Temp = 0;
                        try
                        {
                            int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2));
                        }
                        catch
                        {
                            throw new Exception("128C必须是数字！");
                        }
                        _Text += GetValue(p_Code, p_Text.Substring(0, 2), ref _Temp);
                        _TextNumb.Add(_Temp);
                        p_Text = p_Text.Remove(0, 2);
                    }
                    break;
                case Encode.EAN128:
                    _Examine = 105;
                    if (!((p_Text.Length & 1) == 0)) throw new Exception("EAN128长度必须是偶数");
                    _TextNumb.Add(102);
                    _Text += "411131";
                    while (p_Text.Length != 0)
                    {
                        int _Temp = 0;
                        try
                        {
                            int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2));
                        }
                        catch
                        {
                            throw new Exception("128C必须是数字！");
                        }
                        _Text += GetValue(Encode.Code128C, p_Text.Substring(0, 2), ref _Temp);
                        _TextNumb.Add(_Temp);
                        p_Text = p_Text.Remove(0, 2);
                    }
                    break;
                default:
                    if (p_Code == Encode.Code128A)
                    {
                        _Examine = 103;
                    }
                    else
                    {
                        _Examine = 104;
                    }


                    while (p_Text.Length != 0)
                    {
                        int _Temp = 0;
                        string _ValueCode = GetValue(p_Code, p_Text.Substring(0, 1), ref _Temp);
                        if (_ValueCode.Length == 0) throw new Exception("无效的字符集!" + p_Text.Substring(0, 1).ToString());
                        _Text += _ValueCode;
                        _TextNumb.Add(_Temp);
                        p_Text = p_Text.Remove(0, 1);
                    }
                    break;
            }
            if (_TextNumb.Count == 0) throw new Exception("错误的编码,无数据");
            _Text = _Text.Insert(0, GetValue(_Examine)); //获取开始位


            for (int i = 0; i != _TextNumb.Count; i++)
            {
                _Examine += _TextNumb[i] * (i + 1);
            }
            _Examine = _Examine % 103;           //获得严效位
            _Text += GetValue(_Examine);  //获取严效位
            _Text += "2331112"; //结束位
            Bitmap _CodeImage = isShowNum ? GetImage(_Text) : GetImageZ90(_Text);
            GetViewText(_CodeImage, _ViewText);
            return _CodeImage;
        }
        /// <summary>
        /// 获取目标对应的数据
        /// </summary>
        /// <param name="p_Code">编码</param>
        /// <param name="p_Value">数值 A b  30</param>
        /// <param name="p_SetID">返回编号</param>
        /// <returns>编码</returns>
        private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
        {
            if (m_Code128 == null) return "";
            DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
            if (_Row.Length != 1) throw new Exception("错误的编码" + p_Value.ToString());
            p_SetID = Int32.Parse(_Row[0]["ID"].ToString());
            return _Row[0]["BandCode"].ToString();
        }
        /// <summary>
        /// 根据编号获得条纹
        /// </summary>
        /// <param name="p_CodeId"></param>
        /// <returns></returns>
        private string GetValue(int p_CodeId)
        {
            DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
            if (_Row.Length != 1) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
            return _Row[0]["BandCode"].ToString();
        }
        /// <summary>
        /// 获得条码图形
        /// </summary>
        /// <param name="p_Text">文字</param>
        /// <returns>图形</returns>
        private Bitmap GetImage(string p_Text)
        {
            char[] _Value = p_Text.ToCharArray();
            int _Width = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);
            }
            Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
            Graphics _Garphics = Graphics.FromImage(_CodeImage);

            _Garphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            _Garphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
            _Garphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; //插值模式
            _Garphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;//文本呈现提示
            _Garphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;     //平滑模式

            //Pen _Pen;
            int _LenEx = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);  //获取宽和放大系数
                if (!((i & 1) == 0))
                {
                    //_Pen = new Pen(Brushes.White, _ValueNumb);
                    _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
                }
                else
                {
                    //_Pen = new Pen(Brushes.Black, _ValueNumb);
                    _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
                }
                //_Garphics(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
                _LenEx += _ValueNumb;
            }
            _Garphics.Dispose();
            return _CodeImage;
        }
        /// <summary>
        /// 获得条码图形
        /// </summary>
        /// <param name="p_Text">文字</param>
        /// <returns>图形</returns>
        private Bitmap GetImageZ90(string p_Text)
        {
            char[] _Value = p_Text.ToCharArray();
            int _Width = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);
            }
            Bitmap _CodeImage = new Bitmap((int)m_Height, _Width);
            Graphics _Garphics = Graphics.FromImage(_CodeImage);
            _Garphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; //插值模式
            _Garphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;//文本呈现提示
            _Garphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;     //平滑模式
            //Pen _Pen;
            int _LenEx = 0;
            for (int i = 0; i != _Value.Length; i++)
            {
                int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);  //获取宽和放大系数
                if (!((i & 1) == 0))
                {
                    //_Pen = new Pen(Brushes.White, _ValueNumb);
                    _Garphics.FillRectangle(Brushes.White, new Rectangle(0, _LenEx, (int)m_Height, _ValueNumb));
                }
                else
                {
                    //_Pen = new Pen(Brushes.Black, _ValueNumb);
                    _Garphics.FillRectangle(Brushes.Black, new Rectangle(0, _LenEx, (int)m_Height, _ValueNumb));
                }
                //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
                _LenEx += _ValueNumb;
            }
            _Garphics.Dispose();
            return _CodeImage;
        }
        /// <summary>
        /// 显示可见条码文字 如果小于40 不显示文字
        /// </summary>
        /// <param name="p_Bitmap">图形</param>           
        private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
        {
            if (m_ValueFont == null) return;


            Graphics _Graphics = Graphics.FromImage(p_Bitmap);
            SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
            if (_DrawSize.Height > p_Bitmap.Height - 10 || _DrawSize.Width > p_Bitmap.Width)
            {
                _Graphics.Dispose();
                return;
            }
            //_Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            //_Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;


            int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
            _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
            _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, 0, _StarY);
        }


        //12345678
        //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
        //结果为starc +12 +34 +56 +78 +47 +end



    }
}